package com.aliang.shopping.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.aliang.shopping.common.PageResult;
import com.aliang.shopping.common.ResultCodeEnum;
import com.aliang.shopping.config.OssConfig;
import com.aliang.shopping.contact.SqlColumnConstant;
import com.aliang.shopping.exception.DataException;
import com.aliang.shopping.exception.ServiceException;
import com.aliang.shopping.mapper.UserInfoMapper;
import com.aliang.shopping.model.converter.CommonConverter;
import com.aliang.shopping.model.converter.UserInfoConverter;
import com.aliang.shopping.model.dto.common.PageDTO;
import com.aliang.shopping.model.dto.login.EmailLoginDTO;
import com.aliang.shopping.model.dto.login.LoginDTO;
import com.aliang.shopping.model.dto.login.PhoneLoginDTO;
import com.aliang.shopping.model.enums.UserTypeEnum;
import com.aliang.shopping.model.po.UserInfo;
import com.aliang.shopping.model.vo.CommonUserInfoVO;
import com.aliang.shopping.model.vo.UserInfoVO;
import com.aliang.shopping.security.auth.Payload;
import com.aliang.shopping.service.UserInfoService;
import com.aliang.shopping.util.MailUtil;
import com.aliang.shopping.util.PasswordUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ShiBaLover
 * @description 针对表【user_info(用户信息表)】的数据库操作Service实现
 * @createDate 2024-11-28 10:16:31
 */
@Service
@AllArgsConstructor
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
        implements UserInfoService {

    private final CommonConverter<UserInfoVO> commonConverter = new CommonConverter<>();

    private final UserInfoConverter userInfoConverter;
    private final OssConfig ossConfig;

    @Override
    public PageResult<UserInfoVO> selectPage(PageDTO pageDTO) {
        //查询数据
        IPage<UserInfoVO> page = new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize());
        IPage<UserInfoVO> mapperPage = this.baseMapper.getPage(page, pageDTO);
        //转换数据
        //转换数据
        mapperPage.getRecords().forEach(vo -> {
            if (ObjUtil.isNotNull(vo.getAvatar()))
                vo.setAvatar(ossConfig.getUrl() + vo.getAvatar());
        });
        return commonConverter.toPageResult(mapperPage);
    }


    /**
     * 登录方法
     *
     * @param loginDTO
     * @return
     */
    @Override
    public Payload login(LoginDTO loginDTO) {
        //根据用户名查询用户信息
        UserInfo userInfo = this.baseMapper.selectOne(new QueryWrapper<UserInfo>().eq(SqlColumnConstant.USERNAME, loginDTO.getUsername())
                .eq(SqlColumnConstant.STATUS, SqlColumnConstant.STATUS_OK));
        //判断是否为空
        if (ObjUtil.isEmpty(userInfo)) {
            throw new DataException(ResultCodeEnum.USERNAME_IS_NOT_EXIT);
        }
        // 判断密码是否一致
        if (PasswordUtil.isNotEqual(userInfo.getPassword(), loginDTO.getPassword())) {
            throw new DataException(ResultCodeEnum.PASSWORD_NOT_MATCH);
        }
        return new Payload(userInfo.getId(), UserTypeEnum.USER.getCode());
    }


    @Override
    public Payload emailLogin(EmailLoginDTO loginDTO) {
        UserInfo userInfo = this.baseMapper.selectOne(new QueryWrapper<UserInfo>().eq(SqlColumnConstant.EMAIL, loginDTO.getEmail())
                .eq(SqlColumnConstant.STATUS, SqlColumnConstant.STATUS_OK));
        if (!ObjUtil.isEmpty(userInfo)) {
            //返回载荷
            return new Payload(userInfo.getId(), UserTypeEnum.USER.getCode());
        }
        return null;
    }

    @Override
    public Payload phoneLogin(PhoneLoginDTO loginDTO) {
        UserInfo userInfo = this.baseMapper.selectOne(new QueryWrapper<UserInfo>().eq(SqlColumnConstant.PHONE, loginDTO.getPhone())
                .eq(SqlColumnConstant.STATUS, SqlColumnConstant.STATUS_OK));
        if (!ObjUtil.isEmpty(userInfo)) {
            //返回载荷
            return new Payload(userInfo.getId(), UserTypeEnum.USER.getCode());
        }
        return null;
    }

    @Override
    public CommonUserInfoVO getUserInfo(Long id) {
        UserInfo userInfo = this.baseMapper.selectById(id);
        if (ObjUtil.isEmpty(userInfo)) {
            throw new ServiceException(ResultCodeEnum.USER_NOT_LOGIN);
        }
        return this.userInfoConverter.toCommonUserInfoVO(userInfo);
    }

    @Override
    public void checkPhoneUnique(String phone) {
        Integer count = this.baseMapper.countPhoneAll(phone);
        if (count > 1) {
            throw new DataException(ResultCodeEnum.PHONE_IS_EXIT);
        }
    }

    @Override
    public void checkEmailUnique(String email) {
        Integer count = this.baseMapper.countEmailAll(email);
        if (count > 1) {
            throw new DataException(ResultCodeEnum.EMAIL_IS_EXIT);
        }
    }

    @Override
    public Integer countWithPhone(String phone) {
        return this.baseMapper.countPhoneAll(phone);
    }

    @Override
    public int countWithEmail(String email) {
        return this.baseMapper.countEmailAll(email);
    }


    private final MailUtil mailUtil;
    @Override
    public void sendNoticeMail(List<Long> userId, String msg) {
        // 根据用户id查询用户邮箱
        List<String> mails = this.baseMapper.getMailById(userId);
       // 发送邮箱
        this.mailUtil.sendNoticeBatchEmail(mails, msg);
    }
}




